<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams">
        <el-form-item label="设备型号">
          <el-input v-model="queryParams.equipmentModel" clearable placeholder="请输入设备型号"></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="queryParams.equipmentName" clearable placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item label="设备编号">
          <el-input v-model="queryParams.equipmentNo" clearable placeholder="请输入设备编号"></el-input>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select placeholder="请选择设备状态" v-model="queryParams.equipmentState" clearable>
            <el-option
              v-for="item in equipmentState"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-table border :data="tableData">
        <el-table-column prop="equipmentNo" label="设备编号"></el-table-column>
        <el-table-column prop="equipmentName" label="设备名称"></el-table-column>
        <el-table-column prop="equipmentModel" label="设备型号"></el-table-column>
        <el-table-column prop="equipmentState" label="设备状态">
          <template slot-scope="scope">
            <div>{{ getLabel(scope.row.equipmentState) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click.native.prevent="eqptDetail(scope.row)" type="text" size="small">查看设备信息</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="currentPage"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>

    <position-modal ref="positionModal" @ok="positionModalOk"></position-modal>
    <monitor-modal ref="monitorModal"></monitor-modal>
  </section>
</template>

<script>
import { getEquipmentList, deleteSpot, updateSpot } from './js/patrol'
import PositionModal from './components/PositionModal'
import { equipmentState } from './js/constants'
import MonitorModal from './components/MonitorModal'

export default {
  name: 'Equipment',
  components: { PositionModal, MonitorModal },
  data () {
    return {
      pageSize: 10,
      currentPage: 1,
      totalPageNum: 0,
      tableData: [],
      queryParams: {},
      equipmentState
    }
  },
  mounted () {
    this.getEqptList()
  },
  methods: {
    // 查询按钮
    onSubmit () {
      this.currentPage = 1
      this.getEqptList()
    },

    // 获取巡更点
    async getEqptList () {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }
      const {
        data: { list, total }
      } = await getEquipmentList(Object.assign({}, this.queryParams, params))
      this.tableData = list
      this.totalPageNum = total
    },
    eqptDetail (row) {
      this.$refs.monitorModal.open([this.$pick(row, 'equipmentName', 'equipmentId', 'businessUserId', 'streamInfoId')])
    },

    // 创建巡更点
    addEqpt () {
      this.$refs.positionModal.add()
    },

    // 是否有效
    async changeStatus (row) {
      let params = this.$pick(row, 'id', 'spotEnable')
      await updateSpot(params)
      this.getEqptList()
    },

    // 编辑
    editPosition (row) {
      this.$refs.positionModal.edit(row)
    },

    // 删除
    delEvent (row) {
      this.$confirm('此操作将删除该巡更点,并将删除所有关于该巡更点的巡查任务 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = {
          idList: [row.id]
        }
        const { code, msg } = await deleteSpot(params)
        if (code === 0) {
          this.$message.success('删除成功')
          if (this.tableData.length === 1 && this.currentPage > 1) {
            this.currentPage--
          }
          this.getEqptList()
        } else {
          this.$message.warning(msg)
        }
      })
    },

    // 巡更点CU弹框回调
    positionModalOk () {
      this.getEqptList()
    },

    // 获取label
    getLabel (val) {
      return this.$base.constFormatter(equipmentState, val)
    },

    // 分页
    pageHandleChange (page) {
      this.currentPage = page
      this.getEqptList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-inner {
  .header-card {
    margin-bottom: 10px;
  }
  .el-pagination {
    text-align: center;
    display: block;
    margin: 10px auto;
  }
}
</style>
